#include "data.hpp"
#include "httplib.h"
using namespace httplib;
using namespace std;
namespace aod{
    #define WWWROOT "./www"
    #define VIDEO_ROOT  "/video/"
    #define IMAGE_ROOT  "/image/"
    TableVideo *tb_video=NULL;
    class Server{
        private:
        int _port;
        httplib::Server _svr;
        private:
        static void Insert(const Request &req,Response &rsp)
        {
            if(req.has_file("name")==false||
            req.has_file("info")==false||
            req.has_file("video")==false||
            req.has_file("image")==false)
            {
             rsp.status=400;
             rsp.body=R"({"result":false."reson":"上传的信息有错误"})";
             rsp.set_header("Content-Type","application/json");
             return ;
            }
            MultipartFormData name =req.get_file_value("name");
            MultipartFormData info =req.get_file_value("info");
            MultipartFormData video =req.get_file_value("video");
            MultipartFormData image =req.get_file_value("image");
            string video_name=name.content;
            string video_info=info.content;
            string video_path=WWWROOT;video_path+=VIDEO_ROOT;video_path+=name.content;video_path+=video.filename;
            string image_path=WWWROOT;image_path+=IMAGE_ROOT;image_path+=name.content;image_path+=image.filename;

            if(FileUtil(video_path).SetContent(video.content)==false)
            {
                rsp.status=500;
                rsp.body=R"({"result":false,"reson":"视频文件存储失败"})";
                rsp.set_header("Content-Type","application/json");
                return;
            }
            if(FileUtil(image_path).SetContent(image.content)==false)
            {
                 rsp.status=500;
                rsp.body=R"({"result":false,"reson":"图片文件存储失败"})";
                rsp.set_header("Content-Type","application/json");
                return;
            }
            Json::Value video_json;
            video_json["name"]=video_name;
            video_json["info"]=video_info;
            video_json["video"]=VIDEO_ROOT + video_name + video.filename;;//有问题可以来看看，视频路径是否有误
            video_json["image"]=IMAGE_ROOT + video_name + image.filename;//有问题可以来看看，视频路径是否有误
            if(tb_video->Insert(video_json)==false)
            {
                rsp.status=500;
                rsp.body=R"({"result":false,"reson":"新增数据失败"})";
                rsp.set_header("Content-Type","application/json");
                return ;
            }
            rsp.set_redirect("/index.html",303);
             return ;
        }
        static void Upadte(const Request &req,Response &rsp)
        {
             //1.获取要修改数据的ID
            string vide_id=req.matches[1];
            int video_id=stoi(vide_id);
            Json::Value video;
            if(JsonUtil::UnSerialize(req.body,&video)==false)
            {
                                rsp.status=500;
                rsp.body=R"({"result":false,"reson":"新的视频信息解析失败"})";
                rsp.set_header("Content-Type","application/json");
                return ;
            }
            //2.修改数据库信息
             if(tb_video->Update(video_id,video)==false)
            {
                rsp.status=500;
                rsp.body=R"({"result":false,"reson":"修改数据库信息失败"})";
                rsp.set_header("Content-Type","application/json");
                return ;
            }
            return;
        }
        static void Delete(const Request &req,Response &rsp)
        {
            //1.获取要删除数据的ID
            string vide_id=req.matches[1];
            int video_id=stoi(vide_id);
            //2.删除视频及其文件
            Json::Value video;
            if(tb_video->SelectOne(video_id,&video)==false)
            {
                rsp.status=500;
                rsp.body=R"({"result":false,"reson":"不存在视频信息"})";
                rsp.set_header("Content-Type","application/json");
                return ;
            }
                std::string root = WWWROOT;
				std::string video_path = root + video["video"].asString();
				std::string image_path = root + video["image"].asString();
				remove(video_path.c_str());
				remove(image_path.c_str());

            //3/删除数据库信息
            if(tb_video->Delete(video_id)==false)
            {
                rsp.status=500;
                rsp.body=R"({"result":false,"reson":"删除视频信息失败"})";
                rsp.set_header("Content-Type","application/json");
                return ;
            }
            cout<<"删除视频:"<<video["name"]<<"成功"<<endl;
            return;
        }
        static void SelectOne(const Request &req,Response &rsp)
        {
             //1.获取视频的ID
              string vide_id=req.matches[1];
            int video_id=stoi(vide_id);
             //2.在数据库中查询指定的信息
              Json::Value video;
            if(tb_video->SelectOne(video_id,&video)==false)
            {
                rsp.status=500;
                rsp.body=R"({"result":false,"reson":"查询相应视频信息失败"})";
                rsp.set_header("Content-Type","application/json");
                return ;
            }
            JsonUtil::Serialize(video,&rsp.body);
            rsp.set_header("Content-Type","application/json");
            cout<<"查询视频:"<<video_id<<"成功"<<endl;
            return;
             //3.组织响应正文 Json格式
        }
        static void SelectAll(const Request &req,Response &rsp)
        {
            bool select_flag=true;
            string search_key;
            if(req.has_param("search")==true)
            {
                select_flag=false;
                search_key=req.get_param_value("search");
            }
            Json::Value videos;
            if(select_flag==true)
            {
                if(tb_video->SelectAll(&videos)==false)
            {
                rsp.status=500;
                rsp.body=R"({"result":false,"reson":"查询所有视频信息失败"})";
                rsp.set_header("Content-Type","application/json");
                return ;
            }
            }
            else{
                
                if(tb_video->selectLike(search_key,&videos)==false)
            {
                rsp.status=500;
                rsp.body=R"({"result":false,"reson":"查询匹配视频信息失败"})";
                rsp.set_header("Content-Type","application/json");
                return ;
            }
            }
            JsonUtil::Serialize(videos,&rsp.body);
            cout<<rsp.body<<endl;
            rsp.set_header("Content-Type","application/json");
            cout<<"查询所有成功"<<endl;
            return ;

        }
        public:
        Server(int port):_port(port){};
        bool RunMould()
        {
            //1.初始化操作---初始化数据管理模块，创建指定的目录
            tb_video=new TableVideo();
            FileUtil(WWWROOT).CreateDirectory();
            string video_real_path=WWWROOT;video_real_path+=VIDEO_ROOT;
            FileUtil(video_real_path).CreateDirectory();
            string image_real_path=WWWROOT;image_real_path+=IMAGE_ROOT;
            FileUtil(image_real_path).CreateDirectory();
            //2.搭建http服务器，开始运行
            //(1). 设置静态根目录
            _svr.set_mount_point("/",WWWROOT);
            //(2).添加请求-处理函数映射关系
            _svr.Post("/video",Insert);
            _svr.Delete("/video/(\\d+)",Delete);
            _svr.Put("/video/(\\d+)",Upadte);
            _svr.Get("/video/(\\d+)",SelectOne);
            _svr.Get("/video",SelectAll);
            // 3.启动服务器
            _svr.listen("0.0.0.0",_port);
            return 0;
        }
    };
}
